﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace FRL.DebtChased.Repository
{

	/// <summary>
	/// Declaration of StufferDefinitions class
	/// List of fields that can be inserted in document bodies
	/// </summary>
	public static class StufferDefinitions
	{

		/////////////////////////////////////////////////////////////
		// Constants

		public const string LETTER_PRINTINGDATE = "LETTER_PRINTINGDATE";

		public const string CLIENT_DEARTITLE = "CLIENT_DEARTITLE";
		public const string CLIENT_LETTERADDRESS = "CLIENT_LETTERADDRESS";
			
		public const string DEBTOR_DEARTITLE = "DEBTOR_DEARTITLE";
		public const string DEBTOR_LETTERADDRESS = "DEBTOR_LETTERADDRESS";
			
		public const string ACCOUNT_FRLREF = "ACCOUNT_FRLREF";
		public const string ACCOUNT_CLIENTREF = "ACCOUNT_CLIENTREF";
		public const string ACCOUNT_SUBJECTACCOUNTTOTAL = "ACCOUNT_SUBJECTACCOUNTTOTAL";
		public const string ACCOUNT_SUBJECTCHASINGTOTAL = "ACCOUNT_SUBJECTCHASINGTOTAL";
		public const string ACCOUNT_SUBJECTINVOICECOUNT = "ACCOUNT_SUBJECTINVOICECOUNT";

		public static string[] Fields = new string[]
		{

			LETTER_PRINTINGDATE,

			CLIENT_DEARTITLE,
			CLIENT_LETTERADDRESS,
			
			DEBTOR_DEARTITLE,
			DEBTOR_LETTERADDRESS,
			
			ACCOUNT_FRLREF,
			ACCOUNT_CLIENTREF,
			ACCOUNT_SUBJECTACCOUNTTOTAL,
			ACCOUNT_SUBJECTCHASINGTOTAL,
			ACCOUNT_SUBJECTINVOICECOUNT,
			
		};

		/////////////////////////////////////////////////////////////
		// Operations

		private static string _getSection(string strField)
		{

			// Get the field prefix
			StringBuilder sb = new StringBuilder(
				strField.Substring(0, strField.IndexOf('_')).ToLower()
			);
			sb[0] = char.ToUpper(sb[0]);

			// Return section
			return sb.ToString();

		}

		public static List<string> GetSections()
		{

			// Declare return variable
			List<string> Ftmp = new List<string>();

			// Loop through fields
			for(int i1=0; i1<Fields.Length; i1++ )
			{

				// Get field section
				string strSection = _getSection(Fields[i1]);

				// If needa adding to list
				if (!Ftmp.Contains(strSection))
					Ftmp.Add(strSection);

			}

			// Return variable
			return Ftmp;
	
		}

		public static List<string> GetFields(string strSection)
		{

			// Declare return variable
			List<string> Ftmp = new List<string>();

			// Loop through fields
			for (int i1 = 0; i1 < Fields.Length; i1++)
			{

				// Get field section
				if(strSection == _getSection(Fields[i1]))
					Ftmp.Add(Fields[i1]);

			}

			// Return variable
			return Ftmp;

		}

		public static string AddSyntaxToField(string strField)
		{

			// Format field
			return string.Format("[%{0}%]", strField);
		}

	}

}
